/**
 * paramters:
 * id 
 * title
 * content
 * draggable true
 * width
 * height
 * modal true
 * custom
 * clazz required
 * return objectId
 */
jQuery.idialogCreate = function (paramters){
	var idialogTitle = paramters&&paramters.title?paramters.title:"";
	var idialogContent = paramters&&paramters.content?paramters.content:"";
	var draggable = paramters&&(paramters.draggable!=null)?(paramters.draggable):true;
	var idialogWidth = paramters&&paramters.width?paramters.width:null;
	var idialogHeight = paramters&&paramters.height?paramters.height:null;
	var modal = paramters&&(paramters.modal!=null)?(paramters.modal):true;
	var custom = paramters&&paramters.custom?paramters.custom:null;
	var clazz = paramters&&paramters.clazz?paramters.clazz:"";
	var idialogId = null;
	if (paramters&&paramters.id){
		idialogId = paramters.id;
	}else{
		var date = new Date();
		idialogId = "idialog-"+date.getMinutes()+date.getSeconds()+date.getMilliseconds();
	}
	if(document.getElementById(idialogId)!=null) return idialogId;
	var styleStr = "style='"+(idialogWidth!=null?("width:"+idialogWidth+"px"):"")+";"+(idialogHeight!=null?("height:"+idialogHeight+"px"):"")+"'";
	var templateHtml =
	"<div class='"+clazz+" i-dialog-closable' id='"+idialogId+"'>" +
		"<table "+styleStr+" class='i-dialog' width='100%' border='0' cellpadding='0' cellspacing='0'>" +
			"<tr valign='middle' class='i-dialog-title'>" +
			"<td width='100%' class='i-dialog-title-text'>"+idialogTitle+"</td>" +
			"<td class='i-dialog-close'><a href=\"javascript:jQuery.idialogClose('"+idialogId+"')\">&nbsp;</a></td>" +
			"</tr>" +
			"<tr>" +
			"<td class='i-dialog-body' colspan='2'>" +idialogContent+
			"</td>" +
			"</tr>" +
		"</table>" +
	"</div>";
	if (modal) {
		var coverHtml = "<div class='i-dialog-cover i-dialog-closable'></div>";
		$(document.body).append(coverHtml);
		$(".i-dialog-cover").css("height",$(document).height());
		$(".i-dialog-cover").css("width",$(document).width());
		$(window).scroll(function(){
			$(".i-dialog-cover").css("height",$(document).height());
			$(".i-dialog-cover").css("width",$(document).width());
		});
		$(window).resize(function(){
			$(".i-dialog-cover").css("height",$(document).height());
			$(".i-dialog-cover").css("width",$(document).width());
		});
	}
	if (!custom){
		$(document.body).append(templateHtml);
	}else{
		$(document.body).append("<div class='"+clazz+"' id='"+idialogId+"'>"+custom+"</div>");
	}
	
	var html = $("#"+idialogId);
	html.css("top",$(document).scrollTop()+document.documentElement.clientHeight/2-html.height()/2);
	html.css("left",$(document).scrollLeft()+document.documentElement.clientWidth/2-html.width()/2);
	if (draggable){
		html.draggable({handle:'.i-dialog-title'});
		html.find(".i-dialog-title").css("cursor","move");
	}
	return idialogId;
};

jQuery.idialogClose = function (objectId){
	if (!objectId){
		$(".i-dialog-closable").remove();
	}else{
		$("#"+objectId).remove();
		$("div.i-dialog-cover").remove();
	}
	
};